/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.sale.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import org.letmeshare.stockkeeper.common.Status;
import org.letmeshare.stockkeeper.validator.ValidationException;
import org.letmeshare.stockkeeper.validator.Validator;

/**
 *
 * @author ibisuser1
 */
@Entity
public class Bill implements Serializable, Validator {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    @OneToMany(cascade=CascadeType.ALL)
    private List<BillEntry> entries;
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date billedOn;
    private double discount;
    @Enumerated(EnumType.STRING)
    private Status status;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<BillEntry> getEntries() {
        return entries;
    }

    public void addEntry(BillEntry entry) {
        if (this.status == Status.CANCELLED) {
            throw new IllegalArgumentException("Cannot add entry to a cancelled bill");//TODO throw app exception
        }
        if (this.entries == null) {
            this.entries = new ArrayList<BillEntry>();
        }
        this.entries.add(entry);
        this.status = Status.CREATED;
    }

    public Date getBilledOn() {
        return billedOn;
    }

    public void setBilledOn(Date billedOn) {
        this.billedOn = billedOn;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public double getTotalAmount() {
        double amt = 0;
        if (this.entries != null) {
            for (BillEntry entry: this.entries) {
                amt += entry.getAmount();
            }
        }
        return amt;
    }
    
    public double getPayableAmount() {
        return getTotalAmount() - this.discount;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Bill)) {
            return false;
        }
        Bill other = (Bill) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Bill{" + "id=" + id + ", billed On " + billedOn + ", status " + status + '}';
    }

    

    public void validate() throws ValidationException {
        if (entries == null || entries.isEmpty()) {
            throw new ValidationException("Atleast one entry required");
        }
        for (BillEntry entry : entries) {
            if (entry == null) {
                throw new ValidationException("Invalid entry");
            }
            entry.validate();
        }
        if (billedOn == null) {
            throw new ValidationException("Billed date required");
        }
        if (status == null) {
            throw new ValidationException("Status required");
        }
        if (discount < 0) {
            throw new ValidationException("Invalid discount");
        }
    }
    
}
